package com.sdry.web.controller.jyy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.jyy.Inspection;
import com.sdry.model.jyy.Shedu;
import com.sdry.model.jyy.SheduExample;
import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcInspectionEntity;
import com.sdry.service.jyy.InspectionServise;
import com.sdry.service.zc.ZcTestService;
import com.sdry.utils.PushExample;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName:    [NgController]   
 * @Description:  [不良品登记]   
 * @Author:       [jyy]   
 * @CreateDate:   [2018年10月16日 下午5:26:04]    
 * @Version:      [v1.0]
 */
@RequestMapping("/ng")
@Controller
public class NgController {

	@Resource
	private InspectionServise inspectionServise;
	@Resource
	private ZcTestService ztService;
	
	@RequestMapping("/insert")
	public void insert(HttpServletResponse response, Inspection inspection,HttpServletRequest request) throws Exception {
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		//获取session中的人名
		User people = (User) request.getSession().getAttribute("currentUser");
		
		Date idate = new Date();
		//报检时间
		inspection.setIdate(idate );
		//报检人
		inspection.setIperson(people.getUserName());
		//报检单号
		String format = dateFormat.format(idate);
		inspection.setInumber("BJ"+format);
		//订单状态默认为“0”
		inspection.setIstatus("0");
		
		int insert = inspectionServise.insert(inspection);
		
		if("on".equals(inspection.getRemark01())) {
			if(insert > 0) {
				
				List<String> list = inspectionServise.selectPeople();
				
				for (String s : list) {
					int testSendPush = PushExample.testSendPush("您有一个新的配件报检单", "您有一个新的配件报检单", s, "1");
					System.err.println(testSendPush);
				}
			}
		}
		
		ResponseUtil.write(response, insert);
	}
	
	
	/**
	 * 查询配件报检单列表
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/ngList")
	public void selectSheduList(HttpServletResponse response) throws Exception {
		List<ZcInspectionEntity> list = ztService.selectInspectionList();
    	
		JSONObject jsonObject=new JSONObject();
		if(list.size() > 0) {
	        jsonObject.put("state", 1);
	        jsonObject.put("message", "成功");
	        jsonObject.put("count", list.size());
	        jsonObject.put("data", list);
		}else {
	        jsonObject.put("state", 0);
	        jsonObject.put("message", "无数据");
	        jsonObject.put("count", list.size());
	        jsonObject.put("data", list);
		}
       
        ResponseUtil.write(response, jsonObject);
	}
}
